package cn.wolfcode.crm.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.domain.TotalProductInfo;
import cn.wolfcode.crm.query.ProductQueryObject;
import cn.wolfcode.crm.query.ProductStockQuery;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;

/**
 * @author 丧彪
 */
@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;
    
    @RequiresPermissions("product:index")
    @PermissionName("商品首页")
    @RequestMapping("")
    public String index(){
        return "product/list";
    }

    @RequiresPermissions("product:list")
    @PermissionName("商品列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(ProductQueryObject qo)
    {
        return productService.query(qo);
    }
    
    @RequiresPermissions("product:save")
    @PermissionName("商品保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Product entity){
    	JsonResult result = new JsonResult();
    	try {
    		productService.save(entity);
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("保存失败" + e.getMessage());
		}
        return result;
    }
    
    @RequiresPermissions("product:update")
    @PermissionName("商品更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Product entity){
    	JsonResult result = new JsonResult();
    	try {
    		productService.update(entity);
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("更新失败" + e.getMessage());
		}
        return result;
    }
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            productService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("删除失败");
        }
        return result;
    }

    @RequestMapping("getInfo")
    @ResponseBody
    public TotalProductInfo getInfo(){
        return productService.getInfo();
    }
    
    @RequestMapping("getRepertory")
    @ResponseBody
    public List<HashMap<String, Object>> getRepertory(ProductStockQuery qo){
        return productService.getRepertory(qo);
    }
    
    @RequestMapping("queryBrandInfo")
    @ResponseBody
    public List<Map<String,String>>queryBrandInfo(){
    	return productService.queryBrandInfo();
    }


    @RequestMapping("queryProduct")
    @ResponseBody
    public List<Product> queryProduct(){
        return  productService.queryProduct();
    }
}
